﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PIM_ATE.Electrical
{
    class clsInstrInterface : InstrSwitch
    {
        RFSwitchArray.IRFSwitchArray m_comObj;
        public string m_sComName;
        bool m_isConnected;

        public clsInstrInterface(int iType)
        {
            if (iType == (int)SwitchType.SWITCH_Hxinwb)
            {
                m_comObj = new RFSwitchArray.CRFSwitchArray();
            }
            else
            {

            }
            m_comObj.Enabled = true;
            m_isConnected = false;
            m_iClsType = iType;
        }

        public override bool Connect(string ComName, string path)
        {
            if (GDefine.IS_SWITCH_DEBUG_TEST) return true;
            m_sComName = ComName;
            if (!m_isConnected)
            {
                m_comObj.RFSwitchInit(m_sComName);
                m_isConnected = true;
            }
            return m_isConnected;
        }

        public override bool Close()
        {
            //if (GDefine.IS_SWITCH_DEBUG_TEST) return true;
            //if (m_isConnected)
            //{
            //    m_comObj.CloseSerialPort();
            //    m_isConnected = false;
            //}
            return true;
        }

        public override bool Select_Switch_port(int TestPort1, int TestPort2, int Delay)
        {//华芯微波校准口为0(底层驱动改为最后一个口，与研辰一致)，与输入0通  输入1不通
            if (GDefine.IS_SWITCH_DEBUG_TEST) return true;
            try
            {

                m_sbErr.Length = 0;
                if (m_comObj.OutPortStartIndex == 0)
                {
                    TestPort1 -= 1;
                }
                if (m_comObj.InPortStartIndex == 0)
                {
                    TestPort2 -= 1;
                }
                if (!m_comObj.FeedThroughAbility(m_comObj.InPortStartIndex, TestPort1))
                {
                    m_sbErr.Append(string.Format("开关箱不支持切换！{0}{1}", m_comObj.InPortStartIndex, TestPort1));
                    return false;
                }
                else
                {
                    m_comObj.SyncExchange(m_comObj.InPortStartIndex, TestPort1);
                    //Thread.Sleep(Delay);
                    if (!m_comObj.ExchangeComplete)
                    {
                        m_sbErr.Append(string.Format("开关箱切换失败！{0}{1}", m_comObj.InPortStartIndex, TestPort1));
                        return false;
                    }
                }
                if (!m_comObj.FeedThroughAbility(m_comObj.InPortStartIndex + 1, TestPort2))
                {
                    m_sbErr.Append(string.Format("开关箱不支持切换！{0}{1}", m_comObj.InPortStartIndex + 1, TestPort2));
                    return false;
                }
                else
                {
                    m_comObj.SyncExchange(m_comObj.InPortStartIndex + 1, TestPort2);
                    //Thread.Sleep(Delay);
                    if (!m_comObj.ExchangeComplete)
                    {
                        m_sbErr.Append(string.Format("开关箱切换失败！{0}{1}", m_comObj.InPortStartIndex + 1, TestPort2));
                        return false;
                    }
                }

            }
            catch (Exception EX)
            {
                m_sbErr.Append(string.Format("开关箱切换失败！{0}{1}/n/r  ==>{2}", m_comObj.InPortStartIndex + 1, TestPort2,EX.ToString()));
                return false;
            }
            return true;
        }
    }
}
